package com.tarena.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.tarena.util.datasource.DBConnection;

public class PageEmp {
	public static void main(String[] args){
		new PageEmp().show(2, 10);
	}
	private void show(int pageNo,int pageSize){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn=DBConnection.getConnection();
			String sql = "select * " +
					          "from" +
					               "(select rownum rn ,emp_id,emp_name,salary " +
					               "from " +
				                   	"(select * " +
				                   	"from fuck_emp " +
					                   "order by salary desc" +
					                ") a" +
					           ")b " +
					           "where rn between ? and ?";
			ps=conn.prepareStatement(sql);
			ps.setInt(1, (pageNo-1)*pageSize+1);
			ps.setInt(2, pageNo*pageSize);
			rs=ps.executeQuery();
			while(rs.next()){
				System.out.println(rs.getInt(1)+"\t"+rs.getInt(2));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				DBConnection.close(rs, ps, conn);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
